 # 文件：res://scripts/Card.gd
# Implementation of TECH-T005
extends ColorRect

# 信号：当卡牌被点击时发出
signal card_clicked(card_reference)

# 存储当前卡牌的数据
var card_data: Dictionary = {}

# 初始化函数：使用卡牌数据更新UI
func initialize(data: Dictionary) -> void:
	# 保存数据
	card_data = data
	
	# 添加调试信息
	print("Initializing card with data: ", data)
	
	# 使用get_node获取子节点
	var name_label = $NameLabel
	var type_label = $TypeLabel
	var cost_label = $CostLabel
	var score_label = $ScoreLabel
	var tags_label = $TagsLabel
	
	# 更新各个UI标签
	if name_label:
		name_label.text = data.get("name", "Unknown")
		print("Updated name_label to: ", data.get("name", "Unknown"))
	else:
		print("WARNING: name_label is null!")
	
	if type_label:
		type_label.text = data.get("type", "").capitalize()
		print("Updated type_label to: ", data.get("type", "").capitalize())
	else:
		print("WARNING: type_label is null!")
	
	if cost_label:
		cost_label.text = str(data.get("energy_cost", 0))
		print("Updated cost_label to: ", str(data.get("energy_cost", 0)))
	else:
		print("WARNING: cost_label is null!")
	
	# 根据卡牌类型显示分数或乘数
	if score_label:
		if data.get("type") == "technique":
			var multiplier = data.get("multiplier", 1.0)
			score_label.text = "x" + str(multiplier)
			print("Updated score_label to: x", str(multiplier))
		else:
			score_label.text = str(data.get("base_score", 0))
			print("Updated score_label to: ", str(data.get("base_score", 0)))
	else:
		print("WARNING: score_label is null!")
	
	if tags_label:
		var tags = data.get("tags", [])
		tags_label.text = " ".join(tags) # 将标签数组转换为空格分隔的字符串
		print("Updated tags_label to: ", " ".join(tags))
	else:
		print("WARNING: tags_label is null!")

# 当卡牌被点击时触发
func _on_gui_input(event: InputEvent) -> void:
	if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
		card_clicked.emit(self) # 发出信号，传递自身引用
		print("Card clicked: ", card_data.get("name", "Unknown"))

# 在节点进入场景树时连接信号
func _ready():
	# 连接gui_input信号到_on_gui_input函数
	gui_input.connect(_on_gui_input) 